﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace BanckleChatVisitors
{
    public partial class _Default : System.Web.UI.Page
    {
        string session_resource;
        string api_key;
        string auth_token;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Get_Authorized_Token()
        {

            // Create JSON object to be sent
            JObject request_json = new JObject();
            request_json.Add("userEmail", new JValue(txtEmail.Text));
            request_json.Add("password", new JValue(txtPassword.Text));
            string request_body = request_json.ToString();

            string request_url = "https://apps.banckle.com/api/v2/auth/token";

            // Create request object
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(request_url));
            // Add custom headers for authentication
            request.Headers.Add("banckle-client-api-key", api_key);
           // request.Headers.Add("X-Resource", session_resource);
            // Set request parameters
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = request_body.Length;
            // Add the required JSON to request object
            StreamWriter request_writer = new StreamWriter(request.GetRequestStream());
            request_writer.Write(request_body);
            request_writer.Close();

            // Retrieve response object
            WebResponse response = request.GetResponse();
            Stream response_stream = response.GetResponseStream();
            // Fetch response as string
            string response_body = new StreamReader(response_stream).ReadToEnd();
            // Close response stream to release resources
            response_stream.Close();

            // Parse response as JSON object
            JObject response_json = JObject.Parse(response_body);

            auth_token = response_json["authorization"]["token"].ToString();
            
        }
        protected void Get_Visitors()
        {
            string request_url = "https://chat.banckle.com/v3/visitors/browsing.js";

            // Create request object
            WebRequest request = HttpWebRequest.Create(new Uri(request_url));

            // Add custom headers for authentication
            request.Headers.Add("banckle-client-api-key", api_key);
            request.Headers.Add("X-Resource", session_resource);

            // Retrieve response object
            WebResponse response = request.GetResponse();
            Stream response_stream = response.GetResponseStream();
            // Fetch response as string
            string response_body = new StreamReader(response_stream).ReadToEnd();
            // Close response stream to release resources
            response_stream.Close();

            // Parse response as JSON object
            JObject response_json = JObject.Parse(response_body);

            VisitorsResponse vresp = JsonConvert.DeserializeObject<VisitorsResponse>(response_json.ToString());

            if (vresp.Return.Count > 0)
            {
                lblTotalV.Text = vresp.Return.Count.ToString();
                GridView1.DataSource = vresp.Return;
                GridView1.DataBind();
            }
        }
        protected string Generate_Session()
        {

            string request_url = "http://chat.banckle.com/v3/connect?token=" + auth_token;

            // Create request object
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(request_url));
            // Add custom headers for authentication
            request.Headers.Add("banckle-client-api-key", api_key);

            // Retrieve response object
            WebResponse response = request.GetResponse();
            Stream response_stream = response.GetResponseStream();
            // Fetch response as string
            string response_body = new StreamReader(response_stream).ReadToEnd();
            // Close response stream to release resources
            response_stream.Close();

            // Parse response as JSON array
            JObject session = JObject.Parse(response_body);
            // Fetch session resource from JSON response
            string resource = session["return"]["resource"].ToString();
            return resource;

        }

        protected void Get_Queued_Visitors()
        {

            string request_url = "https://chat.banckle.com/v3/visitors/queue.js";

            // Create request object
            WebRequest request = HttpWebRequest.Create(new Uri(request_url));
            // Add custom headers for authentication
            request.Headers.Add("banckle-client-api-key", api_key);
            request.Headers.Add("X-Resource", session_resource);

            // Retrieve response object
            WebResponse response = request.GetResponse();
            Stream response_stream = response.GetResponseStream();
            // Fetch response as string
            string response_body = new StreamReader(response_stream).ReadToEnd();
            // Close response stream to release resources
            response_stream.Close();

            // Parse response as JSON object
            JObject response_json = JObject.Parse(response_body);

            VisitorsResponse vresp = JsonConvert.DeserializeObject<VisitorsResponse>(response_json.ToString());

            if (vresp.Return.Count > 0)
            {
                lblTotalQ.Text = vresp.Return.Count.ToString();
                GridView2.DataSource = vresp.Return;
                GridView2.DataBind();
            }
            
        }

        public void Get_Operator_Info()
        {
            string online_only = "false";
            string request_url = "https://chat.banckle.com/v3/operators.js?online=" + online_only;

            // Create request object
            WebRequest request = HttpWebRequest.Create(new Uri(request_url));
            // Add custom headers for authentication
            request.Headers.Add("banckle-client-api-key", api_key);
            request.Headers.Add("X-Resource", session_resource);

            // Retrieve response object
            WebResponse response = request.GetResponse();
            Stream response_stream = response.GetResponseStream();
            // Fetch response as string
            string response_body = new StreamReader(response_stream).ReadToEnd();
            // Close response stream to release resources
            response_stream.Close();

            // Parse response as JSON object
            JObject response_json = JObject.Parse(response_body);

            OperatorsResponse vresp = JsonConvert.DeserializeObject<OperatorsResponse>(response_json.ToString());

            if (vresp.Return.Count > 0)
            {
                lblTotalOp.Text = vresp.Return.Count.ToString();
                GridView3.DataSource = vresp.Return;
                GridView3.DataBind();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            try
            {
                api_key = txtKey.Text;
                Get_Authorized_Token();
                session_resource = Generate_Session();

                Get_Visitors();
                Get_Queued_Visitors();
                Get_Operator_Info();
            }
            catch (Exception exp)
            {
                Response.Write(exp.Message);
            }
        }
    }
}
